(function (W, U) {
    "use strict";
    W.txzRefund = {
        CONSTANT: {
            serverUrl: '/web',
            regex: /^[a-zA-Z0-9]{6,20}$/
        },
        _calcDialogTitle: function () {
            var endTime = Date.parse($('#endTime').val());
            var now = new Date();
            var result;
            if (endTime > now) {
                result = false;
            } else {
                result = "请输入退款密码,并确认";
            }
            return result;
        },
        _haveRefundPassword: function (callBack) {
            var that = this
            $.post(this.CONSTANT.serverUrl + "/system/member/haveRefundPassword.do", {}, function (ret) {
                if (ret.success) {
                    callBack();
                } else {
                    layer.prompt({
                        formType: 1, //输入框类型，支持0（文本）默认1（密码）2（多行文本）
                        value: '',
                        title: '请先设置退款密码并妥善保管'
                    }, function (value, index, elem) {
                        if (!that.CONSTANT.regex.test(value)) {
                            util.layerAlertError("密码格式不正确!")
                            return false;
                        }
                        $.post(that.CONSTANT.serverUrl + "/system/member/setRefundPassword.do", {
                            rfp: value
                        }, function (data) {
                            if (data.success) {
                                top.layer.close(index);
                                util.layerAlertSuccess("设置成功!")
                            } else {
                                top.layer.close(index);
                                util.layerAlertError(data.description)
                            }
                        });
                    });
                    var input = $('.layui-layer-prompt').find('.layui-layer-input');
                    $(input).attr('placeholder', '6-20位的数字或字母');
                }
            });
        },
        doRefundOpt: function (ajaxUrl, ajaxParams) {
            var that = this
            var title = this._calcDialogTitle();
            if (!title) {
                util.layerAlertError("该活动在众筹进行中，暂不可退款，待活动结束后方可退款")
                return;
            }
            this._haveRefundPassword(function () {

                layer.open({
                    title: '提示',
                    area: ['300px', title.indexOf('进行中') > -1 ? '220px' : '195px'],
                    content: '<p>' + title + '</p><input type="password" class="layui-layer-input" ' +
                        'style="width: 95%; height: 35px; border: 1px solid #e6e6e6; margin: 0 auto; padding-left: 10px" value=""/>'
                    , btn: ['确定', '取消'],
                    yes: function (index) {
                        var value = $('.layui-layer-dialog').find('.layui-layer-content').find('.layui-layer-input').val();
                        if (!regex.test(value)) {
                            util.layerAlertError("密码格式不正确!")
                            return false;
                        }
                        $.post(that.CONSTANT.serverUrl + "/system/member/verifyRefundPwd.do", {
                            password: value
                        }, function (data) {
                            if (data) {
                                $.post(ajaxUrl, ajaxParams, function (data) {
                                    if (data.success) {
                                        util.layerAlertSuccess("退款申请成功，预计48小时内到账", function () {
                                            window.location.reload();
                                        })
                                    } else {
                                        util.layerAlertError("退款申请异常")
                                    }
                                });
                            } else {
                                util.layerAlertError("密码输入有误!")
                            }
                        });
                    },
                    btn2: function () {
                    }
                });
                $('.layui-layer-dialog').find('.layui-layer-content').find('.layui-layer-input').focus();
            })
        }
    }
}(window, undefined));
